import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'

export default defineConfig(({ command, mode }) => {
  //获取各种环境下的对应的变量
  const env = loadEnv(mode, process.cwd())
  console.log(env.VITE_SERVE, env.VITE_APP_BASE_API)
  return {
    plugins: [
      react(),
    ],
    resolve: {
      alias: {
        '@': './src', // 相对路径别名配置，使用 @ 代替 src
      },
    },
    //代理跨域
    server: {
      proxy: {
        // 添加快手API的代理规则
        '/kuaishou-api': {
          target: 'http://192.168.0.120:3006',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/kuaishou-api/, ''),
        },
        // 添加抖音API的代理规则
        '/douyin-api': {
          target: 'http://192.168.0.120:3006',  
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/douyin-api/, ''),
        },
        // 添加小红书API的代理规则
        '/xiaohongshu-api': {
          target: 'http://192.168.0.120:3006',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/xiaohongshu-api/, ''),
        },
        // 添加GLM-4.5专用代理规则
        '/glm-api': {
          target: 'http://192.168.0.120:3006', // 根据后端实际地址调整
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/glm-api/, '/api/v1/admin/glm-4.5'), // 根据后端实际路径调整
        },
        // 原有API代理规则
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_SERVE,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, ''),
        },
      },
    },
  }
})